#include "os.h"

#include <stdlib.h>
#include <unistd.h>

#if defined WIN32 || defined WIN64
    #include <windows.h>
#elif __linux__
    #include <X11/Xlib.h>
    #include <X11/Xutil.h>
#elif __mac__
    //#include "MacOSX/OSXClipboard.h"
    #include <sys/types.h>
    #include <sys/sysctl.h>
#endif
using namespace std;

namespace sleek
{
    namespace core
    {
        namespace os
        {
            void ClearConsole()
            {
                #if defined __linux
                    system("clear");
                #elif defined WIN32 || defined WIN64
                    system("cls");
                #else
                #endif
            }
            void openURL(char*url)
            {
                #if defined WIN32 || defined WIN64
                    ShellExecute(NULL, NULL,url, NULL, NULL, SW_SHOWNORMAL);
                #elif __linux__
                    //execlp("./"url, "./"url, NULL);
                #else
                    printf("url %s is not open because os not supported", url);
                #endif
            }
            wchar_t*charToWchar_t(const char*msg)
            {
                wchar_t *str = new wchar_t[strsize(msg)];
                mbstowcs(str, msg, strsize(msg)+1);
                return str;
            }
            struct tm*getLocalTimer()
            {
                time_t timer;
                time(&timer);
                struct tm *newTime = localtime(&timer);
                return newTime;
            }
            char*getSystemDate(bool space)
            {
                char *str = new char [80];
                if(space) strftime (str,80,"%d / %m / %Y",getLocalTimer());
                else      strftime (str,80,"%d/%m/%Y",    getLocalTimer());
                return str;
            }
            char*getSystemTime(bool space)
            {
                char *str = new char [80];
                if(space) strftime (str,80,"%H : %M : %S",getLocalTimer());
                else      strftime (str,80,"%H:%M:%S",    getLocalTimer());
                return str;
            }
            char*getUserHome()
            {
                //system( "echo %USERPROFILE%");
                return (char*)"";
            }
            char*getWorkingDirectorie()
            {
//                #if defined WIN32 || defined WIN64
//                    char *buffer = (char*)malloc(PATH_MAX);
//                    char *error = getcwd (buffer,PATH_MAX);
//                    if(!error) return "ERROR";
//                    else       return buffer;
//                #else
                    return (char*)"";
//                #endif
            }
            void Sleeping(unsigned int ms)
            {
                #if defined WIN32 || defined WIN64
                    Sleep(ms);
                #elif __linux__
                    usleep(ms*1000);
                #else
                    //#error undefined os
                #endif
            }
            long strsize(const char *str)
            {
                long i = 0;
                for(;*str; ++str, ++i);
                return i;
            }
            long strsize(const wchar_t *str)
            {
                long i = 0;
                for(;*str; ++str, ++i);
                return i;
            }
            bool checkFileExiste(const char*file)
            {
                bool isOpenned = false;
                std::ifstream myFile;
                    myFile.open(file);
                    isOpenned = myFile.is_open();
                myFile.close();
                return isOpenned;
            }
            const char* print_bool(bool a)
            {
                return a ? "true" : "false";
            }
        }
    }
}
